import math
import gaussian
    
class AutoDensityEstimator:
    """Determines a density of a set of points using a kernel density
    estimator.  Supports incremental addition of points"""
    def __init__(self,bandwidth=0.1):
        self.points = []
        self.density = []
        self.ids = []
        self.bandwidth = bandwidth

    def add(self,newPoint,id):
        g = gaussian.GaussianDistribution(newPoint,[self.bandwidth]*len(newPoint))
        d = g.probability(newPoint)
        for i in xrange(len(self.points)):
            pi = g.probability(self.points[i])
            self.density[i] += pi
            d += pi
        self.density.append(d)
        self.points.append(newPoint)
        self.ids.append(id)

